package com.hhu.mqdemo01;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.hhu.molibs.TRtc;
import com.hhu.utils.THhuMqtt;
import com.hhu.utils.TPeer;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.webrtc.SurfaceViewRenderer;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity {
    public final static String TAG="mqttDemo01";

    public THhuMqtt mqClient;
    public String svrUrl="tcp://10.7.133.149:1883";
    public String uid="admin";
    public String pwd="public";
    public String clientCode ="hhuiot";

    SurfaceViewRenderer localView;
    SurfaceViewRenderer remoteView;

    TRtc rtc = new TRtc();


    TPeer peer;
    TPeer.TPeerConfig peerConfig;
    TPeer.TPeerEvents peerEvents;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openClient();
        loadPeer();
    }
    void loadPeer(){
        localView = findViewById(R.id.localVideo);
        remoteView = findViewById(R.id.remoteView);
        peerConfig = new TPeer.TPeerConfig();
        peerEvents = new TPeer.TPeerEvents(){
            @Override
            public void onInit() {
                super.onInit();
            }
        };
        peer = new TPeer( this, peerConfig);
        peer.init();
    }
    void openClient(){
        THhuMqtt.THhuMqttEvents events = new THhuMqtt.THhuMqttEvents(){
            @Override
            public void onConnected() {
                super.onConnected();
                System.out.println("onConnected");
            }

            @Override
            public void onDisConnected() {
                super.onDisConnected();
                System.out.println("onDisConnected");
            }

            @Override
            public void onReceiveMsg(String topic, String msgContent, Map<String, String> msgInfo) {
                super.onReceiveMsg(topic, msgContent, msgInfo);
                System.out.println("onReceiveMsg:"+topic);
                System.out.println("onReceiveMsg:"+msgContent);
                System.out.println("onReceiveMsg:"+msgInfo);
            }
        };
        mqClient = new THhuMqtt(svrUrl , uid , pwd, clientCode ,events);
        mqClient.open();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }


    @Override
    protected void onDestroy() {
        peer.deInit();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        peer.stopCapture();
    }

    @Override
    protected void onResume() {
        super.onResume();

        peer.startCapture();
    }
}
